#include "StdAfx.h"
#include "ListLabelElementUI.h"
#include "BlueRenderEngineUI.h"
#include "ImagePanelUI.h"


CListLabelElementUI::CListLabelElementUI(void):m_cxWidth(0), m_uTextStyle(DT_VCENTER | DT_NOPREFIX | DT_END_ELLIPSIS), m_uButtonState(0)//,m_hBitmap(NULL)
{
	//LoadProgressImg();
}

CListLabelElementUI::~CListLabelElementUI(void)
{
}

LPCTSTR CListLabelElementUI::GetClass() const
{
	return _T("ListLabelElementUI");
}

void CListLabelElementUI::SetWidth(int cx)
{
	m_cxWidth = cx;
	Invalidate();
}

void CListLabelElementUI::SetTextStyle(UINT uStyle)
{
	m_uTextStyle = uStyle;
	Invalidate();
}

void CListLabelElementUI::Event(TEventUI& event)
{
	if( event.Type == UIEVENT_BUTTONDOWN && IsEnabled() )
	{
		Select();
		Invalidate();
	}
	if( event.Type == UIEVENT_MOUSEENTER )
	{
		m_uButtonState |= UISTATE_HOT;
		Invalidate();
	}
	if( event.Type == UIEVENT_MOUSELEAVE )
	{
		if( (m_uButtonState & UISTATE_HOT) != 0 ) {
			m_uButtonState &= ~UISTATE_HOT;
			Invalidate();
		}
	}
	CListElementUI::Event(event);
}

void CListLabelElementUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if( _tcscmp(pstrName, _T("width")) == 0 ) {
		SetWidth(_ttoi(pstrValue));
	}
	else if( _tcscmp(pstrName, _T("align")) == 0 ) {
		if( _tcsstr(pstrValue, _T("center")) != NULL ) m_uTextStyle |= DT_CENTER;
		if( _tcsstr(pstrValue, _T("right")) != NULL ) m_uTextStyle |= DT_RIGHT;
	}
	else CListElementUI::SetAttribute(pstrName, pstrValue);
}

SIZE CListLabelElementUI::EstimateSize(SIZE /*szAvailable*/)
{
	return CStdSize(m_cxWidth, m_pManager->GetThemeFontInfo(UIFONT_NORMAL).tmHeight + 8);
}

void CListLabelElementUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	DrawItem(hDC, GetPos(), 0);
}

void CListLabelElementUI::DrawItem(HDC hDC, const RECT& rcItem, UINT uDrawStyle)
{
	UITYPE_COLOR iTextColor = UICOLOR_CONTROL_TEXT_NORMAL;
	UITYPE_COLOR iBackColor = UICOLOR__INVALID;
	if( (m_uButtonState & UISTATE_HOT) != 0 ) {
		iTextColor = UICOLOR_CONTROL_TEXT_NORMAL;
		iBackColor = UICOLOR_CONTROL_BACKGROUND_HOVER;
	}
	if( m_bSelected ) {
		iTextColor = UICOLOR_CONTROL_TEXT_SELECTED;
		iBackColor = UICOLOR_CONTROL_BACKGROUND_SELECTED;
	}
	if( (uDrawStyle & UIDRAWSTYLE_INPLACE) != 0 )
	{
		iTextColor = UICOLOR_CONTROL_TEXT_NORMAL;
		iBackColor = UICOLOR_CONTROL_BACKGROUND_NORMAL;
	}
	if( (uDrawStyle & UIDRAWSTYLE_FOCUS) != 0 )
	{
		iTextColor = UICOLOR_CONTROL_TEXT_NORMAL;
		iBackColor = UICOLOR_CONTROL_BACKGROUND_HOVER;
	}
	if( !m_bEnabled ) {
		iTextColor = UICOLOR_CONTROL_TEXT_DISABLED;
		iBackColor = UICOLOR_CONTROL_BACKGROUND_DISABLED;
	}
	// Paint background (because we're vertically centering the text area
	// so it cannot paint the entire item rectangle)
	if( iBackColor != UICOLOR__INVALID ) {
		CBlueRenderEngineUI::DoFillRect(hDC, m_pManager, rcItem, iBackColor);
		
		//CBlueRenderEngineUI::DoPaintBitmap(hDC,m_pManager,m_hBitmap,rcItem);
	}
	// Paint text
	RECT rcText = rcItem;
	::InflateRect(&rcText, -4, 0);
	int nLinks = 0;
	//if(image)
		//image.Draw(hDC,0,0,50,50,0,0,50,50);
	CBlueRenderEngineUI::DoPaintPrettyText(hDC, m_pManager, rcText, m_sText, iTextColor, UICOLOR__INVALID, NULL, nLinks, DT_SINGLELINE | m_uTextStyle);
}

//void CListLabelElementUI::LoadProgressImg()
//{
//	image.Load(L"D:\\Project\\StormWidget\\StormWidgetBaseClassLibrary\\TestStormWidgetUILibaray\\progress.bmp");
//
//	//if( m_hBitmap != NULL ) ::DeleteObject(m_hBitmap);
//	//m_hBitmap = (HBITMAP) ::LoadImage(m_pManager->GetResourceInstance(),L"D:\\storm.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
//	//m_hBitmap = ::CreateBitmap(50,50,1, 32, NULL);
//	////image.Draw()
//	//ASSERT(m_hBitmap!=NULL);
//	//if( m_hBitmap == NULL ) return;
//	//::GetObject(m_hBitmap, sizeof(BITMAP), &m_BitmapInfo);
//}